//
//  EGAdView.m
//  Orchard
//
//  Created by 黄勇锋 on 16/8/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "EGAdView.h"

@interface EGAdView()

@property (strong,nonatomic) UIView *prize;
@property (strong,nonatomic) UIView *seconds;
@property (strong,nonatomic) UIView *redPacket;
@property (strong,nonatomic) UIView *beeGroup;

@property (strong,nonatomic) UIImageView *iv_prize;
@property (strong,nonatomic) UIImageView *iv_seconds;
@property (strong,nonatomic) UIImageView *iv_redPacket;
@property (strong,nonatomic) UIImageView *iv_beeGroup;

@property (strong,nonatomic) UILabel *lb_prize;
@property (strong,nonatomic) UILabel *lb_seconds;
@property (strong,nonatomic) UILabel *lb_redPacket;
@property (strong,nonatomic) UILabel *lb_beeGroup;


@end


@implementation EGAdView


- (void)layoutSubviews{
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.imageViewArray = @[self.iv_prize,self.iv_seconds,self.iv_redPacket,self.iv_beeGroup];
    
    self.labelArray = @[self.lb_prize,self.lb_seconds,self.lb_redPacket,self.lb_beeGroup];
    
    [self setupUI];
}

#pragma mark - 手势实现方法
- (void)tap:(UITapGestureRecognizer *)tap{
    
    UIView *view = tap.view;
    
    [view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    
        if ([obj isKindOfClass:[UILabel class]]) {
            
            UILabel *label = obj;
            
            if (self.adViewBlock) {
                
                self.adViewBlock(label.text);
            }
        }
    }];
    
}


- (void)setupUI{
    
    //添加控件
    [self addSubview:self.prize];
    [self addSubview:self.seconds];
    [self addSubview:self.redPacket];
    [self addSubview:self.beeGroup];
    
    //设置 label
    [self.lb_prize makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.iv_prize);
        make.top.equalTo(self.iv_prize.mas_bottom).offset(10);
    }];
    
    [self.lb_seconds makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.iv_seconds);
        make.top.equalTo(self.iv_seconds.mas_bottom).offset(10);
    }];

    [self.lb_redPacket makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.iv_redPacket);
        make.top.equalTo(self.iv_redPacket.mas_bottom).offset(10);
        
    }];
    
    [self.lb_beeGroup makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.iv_beeGroup);
        make.top.equalTo(self.iv_beeGroup.mas_bottom).offset(10);
    }];

    //设置 imageView
    [self.iv_prize makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.prize).offset(15);
        make.leading.trailing.equalTo(self.prize);
        make.height.equalTo(50);
    }];
    
    [self.iv_seconds makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.seconds).offset(15);
        make.leading.trailing.equalTo(self.seconds);
        make.height.equalTo(50);
    }];

    [self.iv_redPacket makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.redPacket).offset(15);
        make.leading.trailing.equalTo(self.redPacket);
        make.height.equalTo(50);
    }];

    [self.iv_beeGroup makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.beeGroup).offset(15);
        make.leading.trailing.equalTo(self.beeGroup);
        make.height.equalTo(50);
        
    }];
   
    //设置约束
    [self.prize makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(25);
        make.top.bottom.equalTo(self);
        make.width.equalTo(60);
    }];

    [self.seconds makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(self);
        make.width.equalTo(60);
        make.leading.equalTo(self.prize.mas_trailing).offset(28);
    }];
    
    
    [self.redPacket makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.equalTo(60);
        make.top.bottom.equalTo(self);
        make.trailing.equalTo(self.beeGroup.mas_leading).offset(- 28);
        
    }];

    [self.beeGroup makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.top.equalTo(self);
        make.trailing.equalTo(self).offset(- 25);
        make.width.equalTo(60);
    }];
}

- (UIView *)prize{
    
    if (_prize == nil) {
        
        _prize = [[UIView alloc]init];
        
        UITapGestureRecognizer *prizeGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        
        [_prize addGestureRecognizer:prizeGesture];
        
    }
    return _prize;
}

- (UIView *)seconds{
    
    if (nil == _seconds) {
        
        _seconds = [[UIView alloc]init];
        
         UITapGestureRecognizer *secondsGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        
        [_seconds addGestureRecognizer:secondsGesture];
    }
    return _seconds;
}

- (UIView *)redPacket{
    
    if (nil == _redPacket) {
        
        _redPacket = [[UIView alloc]init];
        
        UITapGestureRecognizer *redPacketGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        
        [_redPacket addGestureRecognizer:redPacketGesture];
    }
    
    return _redPacket;
}

- (UIView *)beeGroup{
    
    if (nil == _beeGroup) {
        
        _beeGroup = [[UIView alloc]init];
        
        UITapGestureRecognizer *beeGroupGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        
        [_beeGroup addGestureRecognizer:beeGroupGesture];
    }
    return _beeGroup;
}

- (UIImageView *)iv_prize{
    
    if (_iv_prize == nil) {
        _iv_prize = [[UIImageView alloc]init];
        
    }
    
    [self.prize addSubview:_iv_prize];
    return _iv_prize;
}

- (UIImageView *)iv_seconds{
    
    if (nil == _iv_seconds) {
        _iv_seconds = [[UIImageView alloc]init];

        [self.seconds addSubview:_iv_seconds];
    }
    return _iv_seconds;
}

- (UIImageView *)iv_redPacket{
    
    if (_iv_redPacket == nil) {
        _iv_redPacket = [[UIImageView alloc]init];
    
        [self.redPacket addSubview:_iv_redPacket];
    }
    return _iv_redPacket;
}

- (UIImageView *)iv_beeGroup{
    
    if (nil == _iv_beeGroup) {
        _iv_beeGroup = [[UIImageView alloc]init];

        [self.beeGroup addSubview:_iv_beeGroup];
    }
    return _iv_beeGroup;
}

- (UILabel *)lb_prize{
    if (_lb_prize == nil) {
        _lb_prize = [[UILabel alloc]init];
        _lb_prize.text = @"prize";
        [self.prize addSubview:_lb_prize];
    }
    return _lb_prize;
}

- (UILabel *)lb_seconds{
    if (_lb_seconds == nil) {
        _lb_seconds = [[UILabel alloc]init];
        _lb_seconds.text = @"seconds";
        [self.seconds addSubview:_lb_seconds];
    }
    return _lb_seconds;
}

- (UILabel *)lb_redPacket{
    if (_lb_redPacket == nil) {
        _lb_redPacket = [[UILabel alloc]init];
        _lb_redPacket.text = @"redPacket";
        [self.redPacket addSubview:_lb_redPacket];
    }
    return _lb_redPacket;
}

- (UILabel *)lb_beeGroup{
    if (_lb_beeGroup == nil) {
        _lb_beeGroup = [[UILabel alloc]init];
        _lb_beeGroup.text = @"beeGroup";
        [self.beeGroup addSubview:_lb_beeGroup];
    }
    return _lb_beeGroup;
}


@end
